package com.baseinfo.action;

import com.baseinfo.service.ContractService;
import com.enums.ReturnCode;
import com.framework.base.controller.BaseViewController;
import com.framework.view.KxdParams;
import com.framework.view.ResultDatas;
import com.framework.view.UserInfoView;
import com.framework.view.ResultDatas;
import com.model.TContract;
import com.model.TSysRole;
import com.utils.ReturnCodeUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author admin
* @date 
*/
@Controller
@RequestMapping("/soa/baseinfo/contract")
public class ContractController extends BaseViewController {
Logger logger = Logger.getLogger(ContractController.class);

@Resource(name = "contractServiceImpl")
ContractService service;

/**
* 跳转到首页
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value = "index", method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public ModelAndView query (HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String,Object> params = new HashMap<String,Object>();
UserInfoView<TSysRole> userInfo = getSessionUser(request);
    params.put("user", userInfo);
    return new ModelAndView("/baseinfo/Contract/index", params);
    }

    /**
    * 分页查询所有的
    *
    * @param request
    * @param response
    * @return
    * @throws IOException
    */
    @RequestMapping(value = "datas/search", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object conditionsPaging (HttpServletRequest request, HttpServletResponse response, KxdParams params, Long start, Long length) throws IOException {
    ResultDatas resultDatas = new ResultDatas();
    List<TContract> list = null;
        Long recordsTotal = null;
        Long recordsFiltered = null;

        if (null == params.getParams()) {
        recordsTotal = service.getTotalSize();
        //filter的数额是显示在DT的条目数
        recordsFiltered = recordsTotal;
        list = service.searchPage(start, length);
        } else {
        Map<String,Object> map = params.getParams();
        recordsTotal = service.getSearchSize(map);
        //filter的数额是显示在DT的条目数
        recordsFiltered = recordsTotal;
        list = service.conditionsPage(map, start, length);
        }
        if (list.size() > 0) {
        resultDatas.setSuccess(0);
        resultDatas.setMsg("拿到数据");
        resultDatas.setStart(start);
        resultDatas.setLength(length);
        resultDatas.setRecordsFiltered(recordsFiltered);
        resultDatas.setRecordsTotal(recordsTotal);
        resultDatas.setData(list);
        } else {
        resultDatas.setSuccess(1);
        resultDatas.setData("");
        resultDatas.setMsg("没有数据");
        }
        list = null;
        return resultDatas;

        }

    /**
    * 新增方法(主要做页面跳转到模态框取值)
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "inst", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView add (HttpServletRequest request, HttpServletResponse response) {

    return editModul(request, response, null);//撞到编辑页面
    }

    /**
    * 修改方法
    *
    * @param request
    * @param response
    * @param id
    * @return
    */
    @RequestMapping(value = "edit", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView editModul (HttpServletRequest request, HttpServletResponse response, Long id) {
    TContract model = null;
    if (id != null) {
    model = service.findById(id);
    } else {
    model = new TContract();

    }

    return new ModelAndView("/baseinfo/Contract/edit", "model", model);
    }


    /**
    * 保存对象，inst和edit最后都要走到这里
    *
    * @param request
    * @param response
    * @param modelParam
    * @return
    * @throws IOException
    * @throws ParseException
    */
    @RequestMapping(value = "datas/save", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object save (HttpServletRequest request, HttpServletResponse response, TContract modelParam) throws IOException, ParseException {
    UserInfoView userInfo = this.getSessionUser(request);
    ResultDatas resultDatas = null;
    try {
    if (modelParam.getId() != null) {
    TContract model = service.findById(modelParam.getId());
            model.setName(modelParam.getName());
            model.setContractNo(modelParam.getContractNo());
            model.setProjectNo(modelParam.getProjectNo());
            model.setLeasingPrice(modelParam.getLeasingPrice());
            model.setActualAmount(modelParam.getActualAmount());
            model.setFirstPaymentRatio(modelParam.getFirstPaymentRatio());
            model.setFirstPayment(modelParam.getFirstPayment());
            model.setAlsoRentWay(modelParam.getAlsoRentWay());
            model.setRepaymentPeriod(modelParam.getRepaymentPeriod());
            model.setLoanTime(modelParam.getLoanTime());
            model.setLiftingDay(modelParam.getLiftingDay());
            model.setCumulativeAmount(modelParam.getCumulativeAmount());
            model.setTotalInterest(modelParam.getTotalInterest());
            model.setInterestAte(modelParam.getInterestAte());
            model.setProcessingRate(modelParam.getProcessingRate());
            model.setProcessingAmount(modelParam.getProcessingAmount());
            model.setNominalPrice(modelParam.getNominalPrice());
            model.setIrr(modelParam.getIrr());
            model.setRemark(modelParam.getRemark());
            model.setCreateTime(modelParam.getCreateTime());
            model.setRepaymentStatus(modelParam.getRepaymentStatus());
            model.setIsAbolition(modelParam.getIsAbolition());
    service.saveOrupdate(model);
    resultDatas = new ResultDatas(0,"操作成功",model);
    modelParam = null;
    model = null;
    return resultDatas;
    } else {
    modelParam.setIsDeleted(0);
    modelParam.setCreateTime(new Date());
    modelParam = service.save(modelParam);
    resultDatas = new ResultDatas(0,"操作成功",modelParam);
    modelParam = null;
    return resultDatas;
    }
    } catch (Exception e) {
    e.printStackTrace();
    resultDatas = new ResultDatas(1,"操作失败",null);
    return resultDatas;
    }
    }





     @RequestMapping(value = "datas/del", method = { RequestMethod.POST, RequestMethod.GET })
     @ResponseBody
     public Object del (HttpServletRequest request, HttpServletResponse response, Long id) throws IOException {
     Map<String,Object> result = new HashMap<String,Object>();
     try {
    if (id != null) {
     service.del(id);
    result.put("msg", "删除成功");
    result.put("status", 0);
    } else {
    result.put("msg", "删除失败");
    result.put("status", 1);
    }
    } catch (Exception e) {
     e.printStackTrace();
     result.put("msg", "删除失败");
     result.put("status", 1);
                                }
     return result;
     }


}
